package com.mapler.fp.ui;

import com.mapler.neteller.ui.*;
import com.mapler.neteller.service.NetellerHotMailHelper;
import com.mapler.ngfx.ui.*;
import com.mapler.utility.HttpHelper;
import com.mapler.utility.IConstant;
import java.net.URL;
import java.util.List;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Node;

/**
 *
 * @author none
 */
public class MailParseUI extends javax.swing.JPanel {

    private String username;
    private String password;
    private String country;
    private static Logger log = Logger.getLogger(AddNGFXAccUI.class);

    public MailParseUI() {
        initComponents();
    }

    public MailParseUI(String username, String password, String country) {
        this.username = username;
        this.password = password;
        this.country = country;
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        message = new javax.swing.JLabel();
        generate = new javax.swing.JButton();

        message.setText("Message");

        generate.setText("Generate");
        generate.setToolTipText("");
        generate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                generateActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(60, 60, 60)
                        .addComponent(message, javax.swing.GroupLayout.PREFERRED_SIZE, 429, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(215, 215, 215)
                        .addComponent(generate, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(92, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(message, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(132, 132, 132)
                .addComponent(generate)
                .addGap(0, 206, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void generateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_generateActionPerformed
        try {
            this.message.setText("Working...");
            String uri = "http://" + IConstant.HOSTNAME + "/index.php?r=netellerAccount/getAccounts&username="
                    + this.username + "&password=" + this.password + "&working=0";
            String response = HttpHelper.post(new URL(uri), "");
            Document document = DocumentHelper.parseText(response);
            String errorCode = document.valueOf("Response/errorcode");
            int count = 0;

            if (!errorCode.equalsIgnoreCase("000")) {

                List<Node> list = document.selectNodes("/Response/accounts/account");
                for (Node node : list) {
                    try {
                        NetellerHotMailHelper.readHotmail(username, password, node.valueOf("id"), node.valueOf("email"), node.valueOf("email_pass"));
                        count = count + 1;
                    } catch (Throwable ex) {
                        ex.printStackTrace();
                    }
                }


            } else {
                String msg = document.valueOf("Response/message");
                this.message.setText(msg);
            }
            this.message.setText("Done :" + count);
        } catch (Exception ex) {
            this.message.setText("Error " + ex.getMessage());
            this.message.setText(ex.getMessage());
        }
    }//GEN-LAST:event_generateActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton generate;
    private javax.swing.JLabel message;
    // End of variables declaration//GEN-END:variables

    public void clearFields() {
        try {
            message.setText("");
        } catch (Throwable ex) {
            ex.printStackTrace();
            log.debug("clearFields:: Error causes " + ex);
        }
    }
}
